<?php
/**
 * 产品分类公共模型
 * @author shipanpan
 *
 */
class Application_Model_ProductCatalog extends Model_Base {

	CONST TABLE_NAME = 'p_product_catalog';
	/**
	 * 获取所有分类
	 * @param  array $field [description]
	 * @return [type]        [description]
	 */
	public function getAll(array $field = ["*"]) {
		$db = $this->db();
		$select = $db->select();
		$select->from('p_product_catalog', $field);
		return $db->fetchAll($select);
	}

	/**
	 * 根据分类id获取分类详情
	 * @param  int    $catalogId [description]
	 * @param  array $field     [description]
	 * @return [type]            [description]
	 */
	public function getDetailById(int $catalogId, array $field = ["*"]) {
		$db = $this->db();
		$select = $db->select();
		$select->from('p_product_catalog', $field)->where('id = ?', $catalogId);
		return $db->fetchRow($select);
	}

	/**
	 * 获取分类Id的全称
	 * (全称是指包含分类的父类名称，格式:"顶级父类名称|次级父类名称|分类名称")
	 * @param  [type] $catalogId         分类id，支持批量获取
	 * @param  string $levelNameSeparate 各级分类名称之间的分割符
	 * @return 返回以分类id为键的数组
	 */
	public function getCatalogFullName($catalogId, $levelNameSeparate = "|") {
		//根据查询的分类id的，获取所有与他们相关联的父分类信息，然后将分类的父级拼接
		$catalogId = is_array($catalogId) ? $catalogId : array($catalogId);
		$db = $this->db();
		if (empty($catalogId)) {
			return $this->_error("未指定分类id");
		}

		$searchCatalog = $db->query("select id,name_ch,path from p_product_catalog where " . $db->quoteInto('id in(?)', $catalogId))->fetchAll();
		//根据分类id的path获取他的父分类
		$searchRelateCatalogId = [];
		foreach ($searchCatalog as $oneCatalog) {
			$_catalogPath = $oneCatalog['path'];
			if ($_catalogPath) {
				$searchRelateCatalogId = array_merge($searchRelateCatalogId, explode(',', $_catalogPath));
			}
		}
		unset($catalogPath);
		$searchRelateCatalogId = array_unique($searchRelateCatalogId);
		if (empty($searchRelateCatalogId)) {
			return $this->_error("未找到相关分类id");
		}

		$allCatalog = $db->query("select id,name_ch,path from p_product_catalog where " . $db->quoteInto('id in(?)', $searchRelateCatalogId))->fetchAll();
		$allCatalog = array_column($allCatalog, null, 'id');

		$allCatalogfullName = [];
		foreach ($catalogId as $_oneCatalogId) {
			$fullName = "";
			if (isset($allCatalog[$_oneCatalogId])) {
				$catalogPath = $catalogPath = $allCatalog[$_oneCatalogId]['path'];
				$catalogPathArr = explode(",", $catalogPath);
				if (empty($catalogPathArr)) {
					continue;
				}
				//移除所有分类的父分类“0”id
				array_shift($catalogPathArr);
				if (empty($catalogPathArr)) {
					continue;
				}
				foreach ($catalogPathArr as $pathId) {
					$fullName .= $levelNameSeparate . (isset($allCatalog[$pathId]['name_ch']) ? $allCatalog[$pathId]['name_ch'] : "未知");
				}
				$fullName = ltrim($fullName, $levelNameSeparate);
			}
			$allCatalogfullName[$_oneCatalogId] = $fullName;
		}
		return $allCatalogfullName;
	}

	/**
	 * 获取子分类
	 * @param  [type] $parentId 父分类id
	 * @param  array  $field 返回字段
	 * @return [type]           [description]
	 */
	public function getChildCatalog($parentId, array $field = ['*']) {
		$db = $this->db();
		$select = $db->select();
		$select->from('p_product_catalog', $field)->where("is_enable=?", 1)->where("parent_id = ?", $parentId)->order('order_id');
		return $db->fetchAll($select);
	}

	/**
	 * 获取所有的子分类(包括子分类的子分类......)
	 * @param  [type] $parentId 父分类id
	 * @param  array  $field 返回字段
	 * @return [type]           [description]
	 */
	public function getAllChildCatalog($parentId, array $field = ["*"]) {
		$db = $this->db();
		$select = $db->select();
		$select->from('p_product_catalog', $field)->where("FIND_IN_SET(?,path)", $parentId)->where('is_enable=?', 1);
		return $db->fetchAll($select);
	}

	/**
	 * 根据分类层级获取分类
	 * @param  integer $level 层级(顶级分类层级为0，次级为1,以次类推)
	 * @param  array $field 返回字段
	 * @return array
	 */
	public function getCatalogByLevel($level = 0, array $field = ["*"]) {
		$db = $this->db();
		$select = $db->select();
		$select->from('p_product_catalog', $field)->where("level = ?", $level);
		return $db->fetchAll($select);
	}

	/**
	 * 取产品分类
	 */
	public function getProductCates($param = []) {

		if (count($param) == 0) {
			$param[] = '*';
		}
		return $this->db()->fetchAll('SELECT ' . implode(',', $param) . ' FROM ' . self::TABLE_NAME);
	}

	public function getProductCatesByParam($pid = 0, $fields = ['*']) {

		return $this->db()->fetchAll('SELECT ' . implode(',', $fields) . ' FROM ' . self::TABLE_NAME . ' WHERE parent_id = ' . $pid);
	}
}